const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const { PORT } = require("./config/config.default");
const router = require("./router/index");
const errorHandler = require("./middleware/errorHandler");

const app = express();

// 配置常用中间件
app.use(morgan("dev"));
app.use(cors());
app.use(express.json());

// 挂载路由
app.use(
  "/api",
  (req, res, next) => {
    console.log("GET请求参数是：", req.query);
    console.log("POST请求体是：", req.body);
    // console.log(res);
    next();
  },
  router
);

// 统一进行错误处理
app.use(errorHandler);

app.listen(PORT, (err) => {
  if (err) {
    console.log("服务器监听部分出错了(在app.js)", err);
    return;
  }
  console.log("服务器启动成功，端口为", PORT);
});
